package com.ruoyi.tcm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tcm.domain.TcmKnowledge;
import com.ruoyi.tcm.service.ITcmKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 知识Controller
 *
 * @author cong
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/tcm/knowledge")
public class TcmKnowledgeController extends BaseController {
    @Autowired
    private ITcmKnowledgeService tcmKnowledgeService;

    /**
     * 查询知识列表
     */
    @PreAuthorize("@ss.hasPermi('tcm:knowledge:list')")
    @GetMapping("/list")
    public TableDataInfo list(TcmKnowledge tcmKnowledge) {
//        startPage();
        List<TcmKnowledge> list = tcmKnowledgeService.selectTcmKnowledgeList(tcmKnowledge);
        return getDataTable(list);
    }

    /**
     * 导出知识列表
     */
    @PreAuthorize("@ss.hasPermi('tcm:knowledge:export')")
    @Log(title = "知识", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TcmKnowledge tcmKnowledge) {
        List<TcmKnowledge> list = tcmKnowledgeService.selectTcmKnowledgeList(tcmKnowledge);
        ExcelUtil<TcmKnowledge> util = new ExcelUtil<TcmKnowledge>(TcmKnowledge.class);
        util.exportExcel(response, list, "知识数据");
    }

    /**
     * 获取知识详细信息
     */
    @PreAuthorize("@ss.hasPermi('tcm:knowledge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tcmKnowledgeService.selectTcmKnowledgeById(id));
    }

    /**
     * 新增知识
     */
    @PreAuthorize("@ss.hasPermi('tcm:knowledge:add')")
    @Log(title = "知识", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcmKnowledge tcmKnowledge) {
        return toAjax(tcmKnowledgeService.insertTcmKnowledge(tcmKnowledge));
    }

    /**
     * 修改知识
     */
    @PreAuthorize("@ss.hasPermi('tcm:knowledge:edit')")
    @Log(title = "知识", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TcmKnowledge tcmKnowledge) {
        return toAjax(tcmKnowledgeService.updateTcmKnowledge(tcmKnowledge));
    }

    /**
     * 删除知识
     */
    @PreAuthorize("@ss.hasPermi('tcm:knowledge:remove')")
    @Log(title = "知识", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tcmKnowledgeService.deleteTcmKnowledgeByIds(ids));
    }
}
